package com.ruoyi.web.controller.erp.accounting;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.erp.accounting.domain.Accounting;
import com.ruoyi.erp.accounting.service.AccountingService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import java.util.List;

import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/accounting")
public class AccountingController extends BaseController {
  @Autowired
  private AccountingService accountingService;
  private String prefix = "erp/accounting";
  @RequiresPermissions("Accounting:accounting:view")
  @GetMapping("/accounting")
  public String list (){
    return prefix+"/list";
  }
  @ApiOperation(value="查询")
  @RequiresPermissions("Accounting:accounting:list")
  @PostMapping("/list")
  @ResponseBody
  public TableDataInfo getList(Accounting accounting){
    List<Accounting> accountingList = accountingService.getAccountingList(accounting);
    return getDataTable(accountingList);
  }

  @GetMapping("/add")
  public String add()
  {
    return prefix + "/addList";
  }
  @ApiOperation(value="添加")
  @RequiresPermissions("Accounting:accounting:add")
  @PostMapping("/insert")
  @ResponseBody
  public AjaxResult inertAccounting(Accounting accounting){
    accounting.setRowkey(UUID.randomUUID().toString());
   accountingService.insertAccounting(accounting);

   return AjaxResult.success("添加成功");
  }

  @ApiOperation(value="删除")
  @RequiresPermissions("Accounting:accounting:remove")
  @PostMapping("/delete")
  @ResponseBody
  public AjaxResult deleteAccounting(String ids){
    return toAjax(accountingService.deleteAccounting(ids));
  }

//  @GetMapping("/edit/{accountingGroup}")
//  public String findAccountingGroup()
//  {
//    return prefix + "/updateList";
//  }
  @ApiOperation(value="修改")
  @RequiresPermissions("Accounting:accounting:edit")
  @PostMapping("update")
  @ResponseBody
  public AjaxResult updateAccounting(Accounting accounting){
    try {
      accountingService.updateAccounting(accounting);
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println(e.getMessage());
      return AjaxResult.error("修改保存失败");
    }
    return AjaxResult.success("修改保存成功");
  }

  @ApiOperation(value="导出")
  @RequiresPermissions("Accounting:accounting:export")
  @PostMapping("export")
  @ResponseBody
  public AjaxResult exportAccounting(Accounting accounting){
    List<Accounting> accountingList = accountingService.getAccountingList(accounting);
    ExcelUtil<Accounting> util = new ExcelUtil<>(Accounting.class);
    return util.exportExcel(accountingList,"会计组");
  }

  @ApiOperation(value="根据accountingGroup查询详情")
  @GetMapping("/edit/{id}")
  public String findAccounting(@PathVariable("id") String accountingGroup, ModelMap mmap)
  {
    mmap.put("accountingGroup", accountingService.findAccounting(accountingGroup));
    return prefix+"/update";
  }
}
